using System;
using System.IO;
using System.Linq;
using HQLAddin.ConfigurationInspector;
using HqlIntellisense.ConfigurationAbstraction;
using NUnit.Framework;
using SharpTestsEx;

namespace HQLAddin.Tests.Configuration
{
	[TestFixture, Explicit("These test must be run explictly and one at the time.... This code run in a short lived appdomain.")]
	public class ConfigurationLookupTests
	{
		//Current project reference to NH3 alpha 2.
		//The above path, has a version of NH2.1.2 
		//We are going to have two NHibernate version in the current App.Domain.

		private readonly string output = Path.Combine(Directory.GetCurrentDirectory(), @"..\..\..\Example\Output");
		private readonly string outputWithConfigErrors = Path.Combine(Directory.GetCurrentDirectory(), @"..\..\..\Example\OutputWithWrongConfiguration");
		private readonly string outputWithHibernateCfgXml = Path.Combine(Directory.GetCurrentDirectory(), @"..\..\..\Example\OutputToTestWithHibernateCfgXml");
		

		[Test]
		public void CanGetErrors()
		{
			var configLookup = new ConfigurationLookup(outputWithConfigErrors);
			configLookup.GetErrors().Should().Not.Be.Empty();
			configLookup.GetErrors().First()
				.Description
				.Should()
				.Be.EqualTo("Could not find a getter for property 'Name111' in class 'HqlExample.Domain.Entities.Item'");
		}


		[Test]
		public void CanGetImports()
		{
			var configLookup = new ConfigurationLookup(output);
			configLookup.GetImports().Should().Not.Be.Empty();
		}

		[Test]
		public void CanGetClasses()
		{
			var configLookup = new ConfigurationLookup(output);
			configLookup.GetClasses().Should().Not.Be.Empty();
		}

		[Test]
		public void WhenClassContainsPropertiesTheyAreWellMapped()
		{
			var configLookup = new ConfigurationLookup(output);
			var persistentClassInformation = configLookup.GetClasses().First();
			persistentClassInformation.Properties.Should().Not.Be.Empty();
		}

		[Test]
		public void WhenClassContainsCollectionThenResultHasElementType()
		{
			var configLookup = new ConfigurationLookup(output);
			var persistentClassInformation = configLookup.GetClasses().First(c => c.EntityName.EndsWith("Category"));
			var property = persistentClassInformation.Properties.First(p => p.Name == "Items");

			property.Value.Should().Be.AssignableTo<IPersistentCollection>();
		}

		[Test]
		public void WhenExportUseHibernateCfgXmlThenLookupShouldWork()
		{
			var configLookup = new ConfigurationLookup(outputWithHibernateCfgXml);
			configLookup.GetClasses().Should().Not.Be.Empty();
			configLookup.GetErrors().Should().Be.Empty();
		}
	}
}